package com.example.hw_demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hw_demo.config.WebFaceCompare;
import com.example.hw_demo.entity.User;
import com.example.hw_demo.service.UserService;
import com.example.hw_demo.mapper.UserMapper;
import com.example.hw_demo.util.Result;
import com.example.hw_demo.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-06-19 08:58:32
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{

    @Autowired
    UserMapper userMapper;
    @Override
    public Result selectPage(User user) {
        //分页
        //1、创建分页对象
        Page<User> page = new Page<User>(user.getPage(),user.getSize());
        //2、查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(user.getUserName()!=null&&!"".equals(user.getUserName())){
            queryWrapper.like("user_name",user.getUserName());
        }
        if(user.getUserAccount()!=null&&!"".equals(user.getUserAccount())){
            queryWrapper.like("user_account",user.getUserAccount());
        }

        //3、分页查询
        userMapper.selectPage(page,queryWrapper);
        List<User> list = page.getRecords();//获取查询数据
        long total = page.getTotal();//总行数
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("list",list);
        hashMap.put("total",total);
        return new Result(200,"查询成功",hashMap);
    }

    @Override
    public Result aiLogin(String s, String newName, String s1) {
     double score;//相似度
     String imgName=null;//头像路径
        //上传的照片与用户所有的头像进行匹配
        //获取所有的头像
        File file =new File(s1);//s1头像路径
        File[] files = file.listFiles();//找出他下面的所有头像
        //遍历这些查找出来的头像
        for(File file1:files){
            //每一张头像与上传照片比较
          String path = file1.getPath();//得到上传头像的路径
            //上传照片的路径
            String path1 =s+newName;
            try {
              score =  WebFaceCompare.imgCompare(path,path1);
              if(score>0.9){
                  imgName ="http://localhost:8888/images/"+file1.getName();
                  break;
              }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        //数据库查找
        if(imgName!=null) {
            System.out.println("----------" + imgName);
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.eq("user_img", imgName);//查询条件
            User user =  userMapper.selectOne(queryWrapper);
           if(user!=null){
               String tokenString = new TokenUtil().createToken(user);//创建tokening并且返回登录成功即可
               return  new Result(200,"登录成功",tokenString);
           }else {
               return  new Result(200,"登陆失败",null);
           }
        }else{
            return  new Result(200,"登陆失败",null);
        }
    }
}